﻿using MediatR;
using TraceabilityApp_Api.Application.Command.Breed_养殖_;
using TraceabilityApp_Api.Application.Command.Slaughter_屠宰_;
using TraceabilityApp_Api.DTO;
using TraceabilityApp_Domain;
using TraceabilityApp_ErrorCode;
using TraceabilityApp_Infrastructure;

namespace TraceabilityApp_Api.Application.Handler.Slaughter_屠宰_
{
    public class GetSlaughterHandler : IRequestHandler<GetSlaughterCommand, ApiResult<SlaughterDTO>>
    {
        private readonly IBase<SlaughterModel> slaughterRepo;

        public GetSlaughterHandler(IBase<SlaughterModel> slaughterRepo)
        {
            this.slaughterRepo = slaughterRepo;
        }
        public Task<ApiResult<SlaughterDTO>> Handle(GetSlaughterCommand request, CancellationToken cancellationToken)
        {
            ApiResult<SlaughterDTO> result=new ApiResult<SlaughterDTO>();
           var slaughter=slaughterRepo.GetAll().Where(x=>!x.IsDeleted);
            if (!string.IsNullOrEmpty(request.Batch))
            {
                slaughter = slaughter.Where(x => x.Batch.Contains(request.Batch));
            }
            var linq = (from x in slaughter
                        select new SlaughterDTO
                        {
                            ActualWeighing = x.ActualWeighing,
                            ArrivalTime = x.ArrivalTime,
                            Batch=x.Batch,
                            SlaughterId=x.SlaughterId,
                            SlaughterTime=x.SlaughterTime,
                        }).FirstOrDefault(x=>x.Batch==request.Batch);
            result.Data = linq;
            result.Code = ApiEnum.OK;
            result.Msg = "屠宰信息查询成功";
            return Task.FromResult(result);
        }
    }
}
